import Api, { Response } from '@/src/lib/api/api'
import UserInfoModel from '@/src/lib/models/userinfo_model'
import Taro from '@tarojs/taro'

export interface UserState {
    isLogin: boolean
    wxUserInfo: Taro.UserInfo
    userInfo: UserInfoModel
}

export default {
    namespace: "user",
    state: {},
    reducers: {
        setWxUserInfo(state: UserState, { payload }) {
            return { ...state, ...{ wxUserInfo: payload } }
        },
        setUserInfo(state: UserState, { payload }) {
            console.log('进入',payload)
            if (payload) {
                let model: UserInfoModel = payload
                return { ...state, ...{ isLogin: model.userid > 0, userInfo: model } }
            }

            return { ...state, ...{ isLogin: false, userInfo: null } }
        }
    },
    effects: {
        *requestWxUserInfo({}, { put, call }) {
            try {
                let ret = yield call(Taro.getUserProfile)
                yield put({ type: "setWxUserInfo", payload: ret.userInfo})
            } catch (error) {
                console.log(error)
            }
        },
        *login({}, { put, call, select }) {
            try {

                let wxret = yield call(Taro.login)
                if (!wxret.code) {
                    Taro.showToast({ title: "登录失败", icon: "none" })
                    return false
                }

                let res: Response = yield call(Api.userWxLogin, {code: wxret.code})
                if (!res.issuccess) {
                    Taro.showToast({ title: res.msg, icon: "none" })
                    return false
                }

                let model: UserInfoModel = res.data
                if (model.nickname.length <= 0) {
                    // 更新数据
                    let wxUserInfo: Taro.UserInfo = yield select(state => state.user.wxUserInfo)

                    res = yield call(Api.userSetUserInfo, {
                        nickname: wxUserInfo.nickName,
                        avatarurl: wxUserInfo.avatarUrl,
                        province: wxUserInfo.province,
                        city: wxUserInfo.city,
                        sex: wxUserInfo.gender,
                        userid: model.userid,
                        token: model.token
                    })

                    if (!res.issuccess) {
                        Taro.showToast({ title: res.msg, icon: "none" })
                        return false
                    }

                    model.nickname = wxUserInfo.nickName
                    model.avatarurl = wxUserInfo.avatarUrl
                    model.province = wxUserInfo.province
                    model.city = wxUserInfo.city
                    model.sex = wxUserInfo.gender
                }

                yield put({
                    type: "setUserInfo",
                    payload: model
                })

                return true

            } catch (error) {
                console.log(error)
            }

            return false
        }
    }
}